﻿using MesBSW.BLL.Mes;
using MesBSW.DAL.MesDB;
using Sunny.UI;
using System;

namespace MesBSW.View.Dialog.Mes
{
    public partial class AddRepairReasonDia : UIEditForm
    {
        private SiteInfo _curr_site = null;
        private ExcepOrderControler _ctrl = new ExcepOrderControler();
        private RepairReason _curr_reason = null;

        /// <summary>
        /// 无参数构造方法
        /// </summary>
        public AddRepairReasonDia()
        {
            InitializeComponent();
        }

        public AddRepairReasonDia(SiteInfo site)
        {
            InitializeComponent();
            this._curr_site = site;
            this.InitControlValue();
        }

        public AddRepairReasonDia(SiteInfo site, RepairReason curr_reason)
        {
            InitializeComponent();
            this._curr_site = site;
            _curr_reason = curr_reason;
            this.InitControlValue();
        }


        private void InitControlValue()
        {
            if (this._curr_site != null && this._curr_site.ID > 0)
            {
                this.txtSite.Tag = this._curr_site.ID;
                this.txtSite.Text = this._curr_site.SiteName.Trim();
            }
            else
            {
                this.txtSite.Tag = 0;
                this.txtSite.Text = "";
            }

            if (this._curr_reason != null)
            {
                this.txtReason.Tag = this._curr_reason.ID;
                this.txtReason.Text = this._curr_reason.Reason.Trim();
                if (this._curr_reason.IsDel == 1)
                {
                    this.rbnYes.Checked = true;
                    this.rbnNo.Checked = false;
                }
                else
                {
                    this.rbnYes.Checked = false;
                    this.rbnNo.Checked = true;
                }
            }
            else
            {
                this.txtReason.Tag = 0;
                this.txtReason.Text = "";
                this.rbnYes.Checked = false;
                this.rbnNo.Checked = true;
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            RepairReason tmpBean = new RepairReason();
            int tmpInt = 0;
            if (this.txtReason.Tag.ToString() != null &&　int.TryParse(this.txtReason.Tag.ToString(), out tmpInt) == true)
            {
                tmpBean.ID = int.Parse(this.txtReason.Tag.ToString());
            }
            else 
            { 
                tmpBean.ID = 0; 
            }

            tmpBean.SiteID = this._curr_site.ID;
            tmpBean.SiteName = this._curr_site.SiteName;
            tmpBean.Reason = this.txtReason.Text.Trim();
            tmpBean.IsDel = this.rbnYes.Checked == true ? 1 : 0;

            try
            {
                int count = this._ctrl.SaveRepairReason(tmpBean);
                UIMessageBox.ShowSuccess("更新成功！");
            }
            catch (Exception ex)
            {
                UIMessageBox.ShowError("更新失败！" + "\r\n" + ex.Message);
            }
        }
    }
}
